<?php
	class GoodsModel extends Model{
		// 查询数据总条数
		// public function getCount($cid,$lid){
		//  	$db=M();
	 //        // $field='gid,main_title,sub_title,price,old_price,buy,goods_img';//显示字段
	 //        if(!empty($cid)&&!empty($lid)){
	 //            $sql="select count(*) tot from group_goods where  cid in({$this->soncids}) and lid in({$this->sonlids})";
	 //        }else{
	 //            if(!empty($cid)){
	 //                $sql=$sql="select count(*) tot from group_goods where cid in({$this->soncids})";
	 //            }
	 //            if(!empty($lid)){
	 //                $sql=$sql="select count(*) tot from group_goods where lid in({$this->sonlids})";
	 //            }
	 //        }
	 //        if(empty($cid)&&empty($lid)){
	 //            $sql="select count(*) tot from group_goods";
	 //        }
	 //        $count=$db->query($sql);
	 //        $tot=$count[0]['tot'];
	 //        return $tot;
		// }
		// // 获得商品总数据
		// public function getGoods($cid,$lid,$firstRow,$listRows){
		//  	$db=M();
	 //        $field='gid,main_title,sub_title,price,old_price,buy,goods_img';//显示字段
	 //        if(!empty($cid)&&!empty($lid)){
	 //            $sql="select {$field} from group_goods where cid in({$this->soncids}) and lid in({$this->sonlids}) limit {$firstRow},{$listRows}";
	 //        }else{
	 //            if(!empty($cid)){
	 //                $sql=$sql="select {$field} from group_goods where cid in({$this->soncids}) limit {$firstRow},{$listRows}";
	 //            }
	 //            if(!empty($lid)){
	 //                $sql=$sql="select {$field} from group_goods where lid in({$this->sonlids}) limit {$firstRow},{$listRows}";
	 //            }
	 //        }
	 //        if(empty($cid)&&empty($lid)){
	 //            $sql="select {$field} from group_goods limit {$firstRow},{$listRows}";
	 //        }
	 //        $goodsrows=$db->query($sql);
	 //        // echo '<pre>';
	 //        // print_r($goodsrows);
	 //        return $goodsrows;
		// }
		// 获得符全条件商品总数
		public function getCount(){
			$result=null;
			$field=array(
				'gid',
				'goods_img',
				'main_title',
				'sub_title',
				'price',
				'old_price',
				'buy'
					);
			// 组合条件
			if(!empty($this->cids) && !empty($this->lids)){//分类和地区都不为空时
				$where='cid in ('.$this->cids.') and lid in ('.$this->lids.')';
			}else{
				if(!empty($this->cids)){//分类不为空时
					$where='cid in ('.$this->cids.')';
				}
				if(!empty($this->lids)){//地区不为空时
					$where= 'lid in ('.$this->lids.')';
				}
			}
			if(empty($this->cids) && empty($this->lids)){//分类和地区同时为空时
				$where='';
			}
			// 价格条件不为空时，加到where条件中
			if(!empty($this->price)){
				$where=$where.' and '.$this->price;
			}
			$result=$this->field($field)->where($where)->count();
			return $result;
		}
		// 获得符合条件的商品数据
		public function getGoods(){
			$result=null;
			$field=array(
				'gid',
				'goods_img',
				'main_title',
				'sub_title',
				'price',
				'old_price',
				'buy',
				'begin_time',
					);
			// 组合条件
			if(!empty($this->cids) && !empty($this->lids)){//分类和地区都不为空时
				$where='cid in ('.$this->cids.') and lid in ('.$this->lids.')';
			}else{
				if(!empty($this->cids)){//分类不为空时
					$where='cid in ('.$this->cids.')';
				}
				if(!empty($this->lids)){//地区不为空时
					$where= 'lid in ('.$this->lids.')';
				}
			}
			if(empty($this->cids) && empty($this->lids)){
				$where='';//分类和地区同时为空
				if(!empty($this->price)){//分类和地区同时为空，价格不为空时
					$where=$this->price;
				}
			}
			// 价格条件不为空时，加到where条件中
			if(!empty($this->price)){
				$where=$where.' and '.$this->price;
			}
			// 排序条件不为空时，排序显示
			if(!empty($this->order)){
				$result=$this->field($field)->where($where)->limit($this->limit)->order($this->order)->select();
			}else{
				$result=$this->field($field)->where($where)->limit($this->limit)->select();
			}
			return $result;
		}

		
	}
?>